<template>
  <el-drawer
    :title="param.id ? translate('editDisHd') : translate('addDisHd')"
    size="900px"
    :visible.sync="isShowModal"
    @close="closeDrawer"
  >
    <div>
      <el-form
        ref="form"
        class="doumee-form"
        :model="param"
        label-width="auto"
        label-suffix="："
        :rules="rules"
      >
        <el-form-item :label="translate('goodsType')" prop="businessType">
          <el-select
            v-model="param.businessType"
            class="w400"
            :disabled="Boolean(param.id)"
            @change="changeType"
          >
            <el-option
              v-for="item in seckillTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <template v-if="param.businessType == '04'">
          <el-form-item
            :label="translate('placechooseCombo')"
            prop="businessId"
          >
            <el-select
              v-model="param.businessId"
              class="w400"
              :disabled="Boolean(param.id)"
              @change="(e) => changeBusiness(e, 'combo')"
            >
              <el-option
                v-for="item in comboList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
        <template v-if="param.businessType == '07'">
          <el-form-item :label="translate('placechooseVenue')" prop="venueId">
            <el-select
              v-model="param.venueId"
              class="w400"
              :disabled="Boolean(param.id)"
              @change="changeVenue"
            >
              <el-option
                v-for="item in venueList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="param.venueId"
            :label="translate('placechooseCourse')"
            prop="businessId"
          >
            <el-select
              v-model="param.businessId"
              class="w400"
              :disabled="Boolean(param.id)"
              @change="(e) => changeBusiness(e, 'course')"
            >
              <el-option
                v-for="item in courseList"
                :key="item.courseId"
                :label="item.courseName"
                :value="item.courseId"
              />
            </el-select>
          </el-form-item>
        </template>
        <template v-if="param.businessType == '19'">
          <el-form-item
            :label="translate('placechooseSCard')"
            prop="businessId"
          >
            <el-select
              v-model="param.businessId"
              class="w400"
              :disabled="Boolean(param.id)"
              @change="(e) => changeBusiness(e, 'special')"
            >
              <el-option
                v-for="item in specialList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>

        <el-form-item :label="translate('disSet')" v-if="param.businessId">
          <el-table
            :header-row-class-name="'table-header'"
            class="doumee-element-table"
            :data="activeGoods"
            border
            fit
          >
            <el-table-column
              align="center"
              :label="translate('mingcheng')"
              prop="name"
              min-width="120"
              show-overflow-tooltip
            />
            <el-table-column
              v-if="param.businessType == '04'"
              align="center"
              :label="translate('deadline')"
              min-width="120"
              prop="nums"
              show-overflow-tooltip
            />
            <el-table-column
              align="center"
              :label="translate('originalCost')"
              prop="price"
              min-width="100"
              show-overflow-tooltip
            />
            <el-table-column
              align="center"
              :label="translate('disCost')"
              width="160"
            >
              <template v-slot="scope">
                <el-input-number
                  v-model="scope.row.discountPrice"
                  :min="0.01"
                  :step="1"
                  class="w100 mr10"
                />
                <!-- <el-input v-model="scope.row.discountPrice" class="w100 mr10" /> -->
                <span>{{ translate("rmb") }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item :label="translate('hdname')" prop="name">
          <el-input
            v-model="param.name"
            class="w400"
            maxlength="50"
            :placeholder="translate('qsrmc')"
          />
        </el-form-item>
        <el-form-item :label="translate('hdsj')" prop="selTime">
          <el-date-picker
            v-model="param.selTime"
            type="datetimerange"
            :range-separator="translate('to')"
            value-format="yyyy-MM-dd HH:mm:ss"
            :start-placeholder="translate('kssj')"
            :end-placeholder="translate('jsrq')"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-form-item>
        <div class="df_ac">
          <el-form-item :label="translate('limitSale')" prop="limitCount">
            <el-radio v-model="param.limitCount" label="0">{{
              translate("noastrict")
            }}</el-radio>
            <el-radio v-model="param.limitCount" label="1">{{
              translate("limitSale")
            }}</el-radio>
          </el-form-item>
          <el-form-item
            v-if="param.limitCount == '1'"
            label-width="0"
            class="ml20"
            prop="maxCount"
          >
            <el-input
              v-model="param.maxCount"
              oninput="value=value.replace(/^(0+)|[^\d]+/g, '').slice(0, 5)"
              class="w100"
            />
            <span class="ml10">{{ translate("part") }}</span>
          </el-form-item>
        </div>
        <div class="df_ac">
          <el-form-item :label="translate('perBuyLimit')" prop="personLimit">
            <el-radio v-model="param.personLimit" label="0">{{
              translate("noastrict")
            }}</el-radio>
            <el-radio v-model="param.personLimit" label="1">{{
              translate("BuyLimit")
            }}</el-radio>
          </el-form-item>
          <el-form-item
            v-if="param.personLimit == '1'"
            label-width="0"
            class="ml20"
            prop="personLimitCount"
          >
            <el-input
              v-model="param.personLimitCount"
              oninput="value=value.replace(/^(0+)|[^\d]+/g, '').slice(0, 5)"
              class="w100"
            />
            <span class="ml10">{{ translate("part") }}</span>
          </el-form-item>
        </div>
        <!-- <el-form-item :label="translate('xnms')" prop="virtual">
          <el-radio v-model="param.virtual" label="0">{{ translate('noLimit') }}</el-radio>
          <el-radio v-model="param.virtual" label="1">到点自动抢光</el-radio>
          <el-date-picker
            v-if="param.virtual == '1'"
            v-model="param.virtualTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :placeholder="translate('nianyueri')"
          />
        </el-form-item> -->
        <el-form-item :label="translate('remarkDesc')" prop="remark">
          <el-input
            v-model="param.remark"
            type="textarea"
            :rows="5"
            :placeholder="translate('remarkonlyShop')"
          />
        </el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          :loading="subLoading"
          @click="onSubmit('form')"
          >{{ translate("qd") }}</el-button
        >
        <el-button @click="closeDrawer">{{ translate("qx") }}</el-button>
      </el-form>
    </div>
  </el-drawer>
</template>

<script>
import {

} from '@/api'
import { rules } from './config'
import { seckillTypeOptions } from '@/utils/config'
import dayjs from 'dayjs'
import {
  mealList,
  venueList,
  courseListByVenue,
  SecKillAdd,
  SecKillEdit,
  SecKillDetail,
  SpeailCardList

} from '@/api'
export default {
  data() {
    return {
      param: {
        limitCount: '0',
        personLimit: '0'
      },
      isShowModal: false,
      subLoading: false,
      seckillTypeOptions,

      comboList: [],
      courseList: [],
      specialList: [],
      venueList: [],
      activeGoods: [],
      rules
    }
  },
  created() {

  },
  methods: {
    onSubmit() {
      const that = this
      const param = { ...this.param, type: '1' }
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (param.selTime && param.selTime.length > 0) {
            param.startTime = param.selTime[0]
            param.endTime = param.selTime[1]
          }
          if (this.activeGoods.length > 0 && this.activeGoods[0].discountPrice) {
            param.discountPrice = this.activeGoods[0].discountPrice
          } else {
            return this.$message.warning(this.translate('placeinputDis'))
          }
          this.subLoading = true
          const fn = param.id ? SecKillEdit : SecKillAdd
          fn({ param })
            .then((res) => {
              // console.log(res)
              that.subLoading = false
              if (res.errorCode !== '000000') return
              if (param.id) {
                this.$message.success(this.translate('editcg'))
                this.isShowModal = false
                this.$emit('success')
              } else {
                this.$message.success(this.translate('addcg'))
                this.isShowModal = false
                this.$emit('success')
              }
            })
            .catch((er) => {
              that.subLoading = false
              console.log(er)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getDetail(row) {
      SecKillDetail({ param: { id: row.id } }).then(res => {
        const obj = res.record
        if (obj.startTime) {
          obj.selTime = [obj.startTime, obj.endTime]
        }
        const temp = [{
          name: obj.businessName,
          price: obj.businessOriginPrice.toFixed(2),
          discountPrice: obj.discountPrice,
          nums: obj.businessInfo
        }]
        this.changeType(obj.businessType, obj.businessId, obj.venueId)
        this.activeGoods = temp
        this.param = { ...obj }
      })
    },
    changeBusiness(e, str) {
      if (str === 'combo') {
        const item = this.comboList.find(i => i.id === e)
        this.$set(this.param, 'name', item.name + ' ' + dayjs().format('YYYY-MM-DD'))
        const temp = [{
          name: item.name,
          price: item.price.toFixed(2),
          // discountPrice: '',
          nums: item.nums || item.indate + this.translate('days')
        }]
        this.activeGoods = temp
      } else if (str === 'special') {
        const item = this.specialList.find(i => i.id === e)
        this.$set(this.param, 'name', item.name + ' ' + dayjs().format('YYYY-MM-DD'))
        const temp = [{
          name: item.name,
          price: item.price.toFixed(2)
          // discountPrice: ''
        }]
        this.activeGoods = temp
      } else if (str === 'course') {
        const item = this.courseList.find(i => i.courseId === e)
        const temp = [{
          name: item.courseName,
          price: item.price
          // discountPrice: ''
        }]
        this.activeGoods = temp
      }
    },
    changeType(val, businessId = '', venueId) {
      this.activeGoods = []
      this.$set(this.param, 'businessId', businessId)
      if (val && val === '04') {
        this.getMealList()
      } else if (val && val === '07') {
        this.getVenue()
        if (venueId) {
          this.changeVenue(venueId)
        }
      } else if (val && val === '19') {
        this.getSpecialList()
      }
    },
    getMealList() {
      mealList({ param: {} }).then((res) => {
        this.comboList = res.recordList || []
      })
    },
    getSpecialList() {
      SpeailCardList({ param: { status: '1' } }).then(res => {
        this.specialList = res.recordList.map(item => {
          item.id = item.id + ''
          return item
        })
      })
    },
    getVenue() {
      venueList({ param: {} }).then(res => {
        this.venueList = res.recordList || []
      })
    },
    changeVenue(val) {
      if (val) {
        courseListByVenue({
          param: {
            courseType: '1',
            venueId: val
          }
        }).then(res => {
          this.courseList = res.recordList || []
        })
      }
    },
    closeDrawer() {
      this.isShowModal = false
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.separate {
  width: 100%;
  height: 1px;
  margin: 20px 0px;
  background-color: #dfe2e8;
}
.tip {
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}

.device-uploader {
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  cursor: pointer;
  border: 1px dashed #ccc;
  border-radius: 8px;
  transition: all 0.3s;
  margin-right: 20px;

  &:hover {
    border-color: $menuActive;

    .uploader-icon {
      color: $menuActive;
    }
  }

  ::v-deep .el-upload {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    .file {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      background-size: contain;
      background-position: center;
      background-repeat: no-repeat;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;

      .delete {
        background-color: rgba(0, 0, 0, 0.5);
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        display: none;
        align-items: center;
        justify-content: center;

        .icon {
          color: #fff;
        }
      }

      &:hover {
        .delete {
          display: flex;
        }
      }
    }
  }

  .uploader-icon {
    color: #aaaaaa;
    font-size: 22px;
    transition: all 0.3s;
  }
}
.count-style {
  display: inline-block;
  width: 45%;
  height: 60px;
}

.division-line {
  text-align: center;
  display: inline-block;
  color: #999;
  width: 50px;
}

.clock {
  display: inline-block;
}

.upload-style {
  vertical-align: middle;
  display: inline-block;
  // width: 60px;
  // height: 60px;
  border: none;
}

.img-style {
  margin: auto;
  background-color: #eee;
  width: 100px;
  height: 60px;
  vertical-align: middle;
}

.avatar {
  width: 60px;
  height: 60px;
  display: block;
}

.line-style {
  background-color: #dfe2e8;
  height: 1px;
  // width: 80%;
  // margin: 0 auto;
  margin-bottom: 20px;
}
</style>
